import Vue from 'vue';
import echarts from 'echarts'; // echarts
export default {
  bind: function(el, binding) {
    Vue.nextTick(() => {
      el.echartsInstance = echarts.init(el);
      el.resizeEventHandler = function() {
        el.echartsInstance.resize();
      };
      if (window.attachEvent) {
        window.attachEvent('onresize', el.resizeEventHandler);
      } else {
        window.addEventListener('resize', el.resizeEventHandler, false);
      }
    });
  },
  update: function(el, binding) {
    Vue.nextTick(() => {
      el.echartsInstance.setOption(binding.value);
    });
  },
  unbind: function(el) {
    el.echartsInstance.dispose();
    if (window.attachEvent) {
      window.detachEvent('onresize', el.resizeEventHandler);
    } else {
      window.removeEventListener('resize', el.resizeEventHandler, false);
    }
  }
};
